package klab;
import java.io.*;
import java.util.*;

public class ReadEnv {
	
	// Singlton instanca
	private static ReadEnv instance=null;
	// proces iz koga se cita output
	private Process p = null;
	// varijable okruzenja
	private Properties envVars = new Properties();
	
	// inicijalizacija pri prvom instanciranju
	private ReadEnv(){
		try {
			refresh();
		} catch (IOException e) {
			e.printStackTrace();
		}
	};
	
	// singlton instanciranje
	public static ReadEnv getInstance(){
		if (instance != null )
			return instance;
		return instance = new ReadEnv();
	}
	
	// osvezava stanje varijabli po zelji
	public void refresh()throws IOException{
		Runtime rt = Runtime.getRuntime();
		String OS = System.getProperty("os.name").toLowerCase();
//		System.out.println(OS);
		if (OS.indexOf("windows 9") > -1) {
			p = rt.exec( "command.com /c set" );
	    }else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 20") > -1 ) || 
				(OS.indexOf("windows xp") > -1) || (OS.indexOf("windows 7") > -1 ) ) {
			p = rt.exec( "cmd.exe /c set" );
	    }else {
	    // pretpostavljamo da je u pitanju Unix
	    	p = rt.exec( "env" );
	    }		
		BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
		String line;
		while( (line = br.readLine()) != null ) {
			int idx = line.indexOf( '=' );
			String key = line.substring( 0, idx );
			String value = line.substring( idx+1 );
			envVars.setProperty( key, value );
//			System.out.println( key + " = " + value );
		}
	}
	
	// vraca sve sistemske varijable okruzenja
	public Properties getEnvVars(){
		return envVars;
	}
	/**
	 * Vraca punu putanju do home direktorijuma koja je svedena na odgovarajuci oblik
	 * @param mrezni
	 * @return
	 */
	public String getMainHomePath(boolean mrezni){
		StringBuffer result = new StringBuffer();  
		String temp1=null,temp2=null;
		try {
			Properties p = getEnvVars();
//			System.out.println("the current value of HOMEPATH is -> " +
//			p.getProperty("HOMEDRIVE") + p.getProperty("HOMEPATH")
			temp1 = p.getProperty("HOMEDRIVE");
			temp2 = p.getProperty("HOMEPATH");
		}
		catch (Throwable e) {
			e.printStackTrace();
		};
     
		// Ako je pritup preko mreze C: se menja sa C$ zbog administratorskog pristupa
		if (mrezni) temp1 = temp1.replace(':', '$');
		result.append(temp1);
		
		// Svuda gde imamo \\ menjamo ga sa /
		temp2 = temp2.replace("\\\"", "/");
		temp2 = temp2.replace("\"\\", "/");
		temp2 = temp2.replace('\"', '/');
		temp2 = temp2.replace('\\', '/');


	
		StringTokenizer backslash = new StringTokenizer(temp2,"\\/");
		for (int i=0;i<backslash.countTokens();i++){
			result.append("\\");
			result.append(backslash.nextToken());
		}
		return result.toString();
	}
	
	// Vraca oznaku Sistemskog Home drajva
	public String getHomeDriveName(){
		StringTokenizer drive = new StringTokenizer(getMainHomePath(false),":");
		return drive.nextToken();
	}

  public static void main(String args[]) {
	  ReadEnv re = new ReadEnv();
	  String whole = re.getMainHomePath(true);
	  try {
	     Properties p = re.getEnvVars();
	     System.out.println("the current value of HOMEPATH is -> " +
	    	p.getProperty("HOMEDRIVE") +
	        p.getProperty("HOMEPATH"));
	  }
	  catch (Throwable e) {
	     e.printStackTrace();
	  }
	  System.out.println();
	  System.out.println(whole);
	  System.out.println(re.getHomeDriveName());
	  System.out.println(whole.substring(3));
   }
}
